// pages/forget/forget.js
const app = getApp(), core = require('../../utils/core'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countdown: 0,
    email: '',
    mobile:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  inputEmail(event) {
    this.setData({email: event.detail.value});
  },
  inputMobile(event) {
    this.setData({mobile: event.detail.value});
  },
  // sendCaptcha: function() {
  //   let page = this;
  //   if(page.data.countdown > 0) {
  //     return;
  //   }
  //   let email = this.data.email;
  //   if(!WeValidator.checkValue('email', email)) {
  //     wx.showToast({title: '请输入注册邮箱',icon:'none'});
  //     return;
  //   }
  //   core.get({url: '/ems/send', data: {email:email,event:'resetpwd'}, success(ret, response) {
  //     page.setData({countdown: 60});
  //     let timer = setInterval(function() {
  //       let tick = page.data.countdown - 1;
  //       page.setData({countdown: tick});
  //       if(tick<=0) {
  //         clearInterval(timer);
  //       }
  //     },1000);
  //   }});
  // },
  sendCaptcha: function() {
    let page = this;
    if(page.data.countdown > 0) {
      return;
    }
    let mobile = this.data.mobile;
    if(!WeValidator.checkValue('mobile', mobile)) {
      wx.showToast({title: '请输入注册手机号',icon:'none'});
      return;
    }
    core.post({url: '/xilujob.sms/send', data: {mobile:mobile,event:'resetpwd'}, success(ret, response) {
      page.setData({countdown: 60});
      let timer = setInterval(function() {
        let tick = page.data.countdown - 1;
        page.setData({countdown: tick});
        if(tick<=0) {
          clearInterval(timer);
        }
      },1000);
    }});
  },

  // bindSubmit(event) {
  //   let data = event.detail.value;
  //   let validator = new WeValidator({
  //     rules: {
  //       email: {required:true,email:true},
  //       captcha: {required:true,digits:true,length:4},
  //       password: {required:true, rangelength:[3,20]},
  //       repeat_password: {required: true, equalTo:'password'}
  //     },
  //     messages: {
  //       email: {required:'请输入注册邮箱',email:'请输入正确的邮箱地址'},
  //       captcha: {required:'请输入邮件收到的验证码',digits:'请输入4位数字验证码',length:'请输入4位数字验证码'},
  //       password: {required:'请输入3-20位密码', rangelength:'请输入3-20位密码'},
  //       repeat_password: {required: '请再次输入密码', equalTo:'两次密码输入不一致'}
  //     }
  //   });
  //   if(!validator.checkData(data)) {
  //     return;
  //   }
  //   data.type = 'email';
  //   core.post({
  //     url: 'user/resetpwd', data: data,
  //     success: ret => {
  //       wx.navigateBack();
  //       wx.showToast({title: '重置密码成功，请登录',icon:'none'});
  //     }
  //   })
  // }

  bindSubmit(event) {
    let data = event.detail.value;
    let validator = new WeValidator({
      rules: {
        mobile: {required:true,mobile:true},
        captcha: {required:true,digits:true,length:4},
        password: {required:true, rangelength:[3,20]},
        repeat_password: {required: true, equalTo:'password'}
      },
      messages: {
        mobile: {required:'请输入注册手机号',email:'请输入正确的手机号'},
        captcha: {required:'请输入邮件收到的验证码',digits:'请输入4位数字验证码',length:'请输入4位数字验证码'},
        password: {required:'请输入3-20位密码', rangelength:'请输入3-20位密码'},
        repeat_password: {required: '请再次输入密码', equalTo:'两次密码输入不一致'}
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    data.type = 'mobile';
    core.post({
      url: '/xilujob.user/resetpwd', data: data,
      success: ret => {
        wx.navigateBack();
        wx.showToast({title: '重置密码成功，请登录',icon:'none'});
      }
    })
  }
});
